/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Form = require("form");
	var store =require("store");
	var activeUser = store.get("user");
	var Dialog = require("dialog");
	var enmu = require("enums");
	
    var instance = {
        initComponent: function (params,widget) {
        	var form = new Form({
        		parentNode: ".J-form",
        		show: false,
        		model: {
        			id: "form",
        			layout: "3_1",
        			defaultButton : false,
        			items: [{
        				name: "memberCertificates",
        				label: "会员证号",
        				type: "text",
        				validate: [ "required" ],
        				events: {
        					"blur":function(e){
								var memberCertificates = e.target.value;
								if(memberCertificates){
									widget.handler.form.searchContact(widget,memberCertificates,null,true);
								}
							}
        				}
        			},{
        				name: "contactNo",
        				label: "合同号",
        				type: "text",
        				validate: ["required"],
        				readonly:true
        				/*events: {
        					"blur":function(e){
								var contactNo = e.target.value;
								if(contactNo){
									widget.handler.form.searchContact(widget,contactNo,null);
								}
							}
        				}*/
        			},{
        				name: "status.value",
        				label: "状态",
        				type: "text",
        				readonly:true
        			},{
        				name: "product.name",
        				label: "产品",
        				type: "text",
        				readonly:true
        			},{
        				name: "productType.name",
        				label: "产品类型",
        				type: "text",
        				readonly:true
        			},{
        				name: "number",
        				label: "份数",
        				type: "text",
        				readonly:true
        			},{
        				name: "totalPrice",
        				label: "总金额(元)",
        				type: "text",
        				readonly:true
        			},{
        				name: "signUpDate",
        				label: "签约日期",
        				type: "date",
        				readonly:true
        			},{
        				name: "balance",
        				label: "余额(元)",
        				type: "text",
        				readonly:true
        			},{
        				name: "balTime",
        				label: "居住期限余额",
        				type: "text",
        				readonly:true
        			},{
        				name:"unit",
						label:"期限单位",
						options: enmu["com.eling.elcms.travel.model.ProductRuleDetail.Unit"],
						readonly:true
        			},{
        				name: "name",
        				label: "会员",
        				type: "text",
        				readonly:true
        			},{
        				name: "idNumber",
        				label: "身份证号",
        				type: "text",
        				readonly:true
        			},{
        				name: "phone",
        				label: "联系电话",
        				type: "text",
        				readonly:true
        			},{
        				name: "defaultRatio",
        				label: "违约金比例(%)",
        				type: "text",
        				validate: ["required"],
						events: {
	        					"change": function(e){ 
	        						var reglen = new RegExp(/^(100|[1-9]\d|\d)$/);
	        						var value =  e.target.value;
	   							    if(reglen.test(value)==true){
	   							    	var form = widget.get("form");
		        						var defaultRatio = form.getValue("defaultRatio")
		        						var flat = form.getValue("flatCost");
		        						if(flat == ""){
		        							form.setValue("flatCost",0);
		        						}
		        						var totalPrice = form.getValue("totalPrice");
		        						var balance = form.getValue("balance");
		        						var flatCost = form.getValue("flatCost");
		        						if(defaultRatio == "0" ){
		        							var realPrices = 0;
		        							realPrices += (parseFloat(balance) - parseFloat(flatCost));
	                						form.setValue("price",realPrices);
	            						}else{
	            							//应退金额=余额-总金额*违约金比例-工本费
	            							var realPrices = 0;
	            							realPrices += (parseFloat(balance) - parseFloat(totalPrice)*parseFloat(defaultRatio)/100 - parseFloat(flatCost));
	                						form.setValue("price",realPrices);
	            						}
	   							 }else{
	   								 Dialog.alert({
                           				 tip : "提示",
                           				 content : "违约金比例(%)必须输入0-100的数字！"
                           			 });
                           			 form.setValue("defaultRatio", "");
                           			 return false;
	   							 }
	        					}
	        				}
        			},{
        				name: "flatCost",
        				label: "工本费(元)",
        				type: "text",
        				validate: ["required"],
        				events: {
        					"change": function(e){
        						 var form = widget.get("form");
        						 var flatCost = form.getValue("flatCost");
                           		 if(!isNaN(flatCost) && parseInt(flatCost) > 0){
                           			form.setValue("flatCost", parseFloat(flatCost).toFixed(2));
            						var defaultRatio = form.getValue("defaultRatio")
            						var flatCost = form.getValue("flatCost");
            						var totalPrice = form.getValue("totalPrice");
            						var balance = form.getValue("balance");
            						if(defaultRatio == "0" ){
            							var price = 0;
                						price += (parseFloat(balance) - parseFloat(flatCost));
                						form.setValue("price",price);
            						}else{
            							//应退金额=余额-总金额*违约金比例-工本费
            							var price = 0;
                						if(defaultRatio != null && defaultRatio != ""){
                							price += (parseFloat(balance) - parseFloat(totalPrice)*parseFloat(defaultRatio)/100 - parseFloat(flatCost));
                						}
                						form.setValue("price",price);
            						}
                           		 } else{
                           			 Dialog.alert({
                           				 tip : "提示",
                           				 content : "工本费必须输入数字且不能为负！"
                           			 });
                           			 form.setValue("flatCost", "");
                           			 return false;
                           		 }
        					}
        				}
        			},{
        				name: "price",
        				type: "text",
        				label: "应退金额(元)",
        				readonly:true,
        			},{
        				name: "operateDate",
        				label: "申请日期",
        				type:"date",
						defaultValue:moment().format("YYYY-MM-DD"),
						readonly:true
        			},{
        				name: "operate",
        				type: "text",
        				label: "经办人",
        				readonly:true,
        				defaultValue:activeUser.name,
        			},{
        				name: "cause",
        				label: "退证原因",
        				type: "textarea",
                        "break": true,
                        exValidate: function(value) {
                            var reg = /^[\u4E00-\u9FA5A-Za-z0-9\-\/\\\.\(\)\~\<\>\[\]\{\}]{1,50}$/;
                            if(value.length == ""){
                            	return true;
                            }else if (!reg.test(value)) {
                                return "只能输入数字、字符、字母和汉字且不能超过50个字!";
                            } else {
                                return true;
                            }
                        }
                         /*exValidate: function(value){
							 var reglen = new RegExp(/^[^\s^\u4E00-\u9FA5A-Za-z0-9]+$/);
							 if(reglen.test(value)==true && value.length<51 ){
								 return true;
							 }else{
								 Dialog.alert({
                       				 tip : "提示",
                       				 content : "只能输入数字、字符、字母和汉字且不能超过50个字！"
                       			 });
                       			 form.setValue("cause", "");
                       			 return false;
							 }
						 }*/
        			},{
        				name:"flowStatus",
						type:"hidden",
						defaultValue:"Initial"
        			},{
        				name:"pkReturnCredentials",
						type:"hidden",
        			},{
        				name:"version",
						type:"hidden",
						defaultValue:"0"
        			},{
        				name:"pkTravelContract",
						type:"hidden",
        			},{
        				name:"operator",
						type:"hidden",
						defaultValue:activeUser.pkUser,
        			},{
        				name:"contractVersion",
						type:"hidden",
        			}]
        		}
        	}); 
        	widget.set("form", form);
        }
    };
    module.exports = instance;
});